package cn.com.dhcc.turbo.service.registry;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.base.IocBean;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.CollectionUtil;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.app.pub.core.service.TurboCacheService;
import cn.com.dhcc.app.pub.core.service.TurboCacheService.KEY_VALUE_TYPE;
import cn.com.dhcc.turbo.entity.exch.GapInfo;
import cn.com.dhcc.turbo.entity.exch.GapUserConf;
import cn.com.dhcc.turbo.entity.registry.AdapterBase;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
import cn.com.dhcc.turbo.entity.registry.DataCategory;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.exch.cmdata.GapInfoService;
import cn.com.dhcc.turbo.service.exch.cmdata.GapOwnerConfService;
import cn.com.dhcc.turbo.service.registry.vo.PipeLineFtp;

/**
 * 基础信息服务实现
 */
@Service(IocBean.BASE_INFO_SERVICEIMPL_BEAN)
public class BaseInfoServiceImpl extends BaseService implements BaseInfoService {
	private static Logger logger = Log.getLogger(LOGTYPE.TURBO);
	@Autowired
	NodeInfoService nodeInfoService;
	@Autowired
	AdapterBaseService adapterBaseService;
	@Autowired
	NodePipelineService nodePipelineService;
	@Autowired
	@Qualifier(AppIocBean.TURBO_CACHE_SERVICE)
	private TurboCacheService turboCacheService;
	@Autowired
	private GapInfoService gapInfoService;
	@Autowired
	private GapUserConfService gapUserConfService;
	@Autowired
	private AdapterInfoService adapterInfoService;
	@Autowired
	private GapOwnerConfService gapOwnerConfService;
	@Autowired
	private DataCategoryService dataCategoryService;
	@Override
	public NodeInfo getCurrentNodeInfo() throws ServiceException {
		NodeInfo nodeInfo = (NodeInfo) turboCacheService.getData("获取当前节点", KEY_VALUE_TYPE.NULL__LOCAL_NODEINFO,  KEY_VALUE_TYPE.NULL__LOCAL_NODEINFO.toString());
		if (nodeInfo == null) {
			try {
				nodeInfo = nodeInfoService.getLocalNode();
			} catch (ServiceException e) {
				throw e;
			}
			
			if(nodeInfo != null){
				turboCacheService.putData("插入当前节点信息缓存对象" , KEY_VALUE_TYPE.NULL__LOCAL_NODEINFO, KEY_VALUE_TYPE.NULL__LOCAL_NODEINFO.toString(), nodeInfo);
			}
		}
		return nodeInfo;
	}

	@Override
	public NodeInfo getNodeInfoByCode(String nodeCode) throws ServiceException {
		NodeInfo nodeInfo = (NodeInfo) turboCacheService.getData("根据编码获取节点", KEY_VALUE_TYPE.NODE_CODE__NODEINFO, nodeCode);
		if (nodeInfo == null) {
			try {
				nodeInfo = nodeInfoService.getByUniqueProperty("node_code", nodeCode);
			} catch (ServiceException e) {
				throw e;
			}
			
			if(nodeInfo != null){
				turboCacheService.putData("插入节点信息缓存对象code:" + nodeCode, KEY_VALUE_TYPE.NODE_CODE__NODEINFO, nodeCode, nodeInfo);
			}
		}
		return nodeInfo;
	}

	@Override
	public NodeInfo getNodeInfoById(String nodeId) throws ServiceException {
		TurboCacheService cacheService = turboCacheService;
		NodeInfo nodeInfo = (NodeInfo) cacheService.getData("根据id获取节点", KEY_VALUE_TYPE.NODE_ID__NODEINFO, nodeId);
		if (nodeInfo == null) {
			try {
				nodeInfo = nodeInfoService.get(nodeId);
			} catch (ServiceException e) {
				throw e;
			}
			if(nodeInfo != null){
				cacheService.putData("插入节点信息缓存节点对象id:" + nodeId, KEY_VALUE_TYPE.NODE_ID__NODEINFO, nodeId, nodeInfo);
			}
		}
		return nodeInfo;
	}



	/*@Override
	public SysServiceInfo getSysServiceInfoBySysId(String sysId) throws ServiceException {
		TurboCacheService cacheService = turboCacheService;
		SysServiceInfo serviceInfo = (SysServiceInfo) cacheService.getData("根据系统ID获取系统服务",KEY_VALUE_TYPE.SYS_ID__SYSSERVICE, sysId);
		if (serviceInfo == null) {
			try {
				serviceInfo = sysServiceInfoService.getBySysId(sysId);
			} catch (ServiceException e) {
				throw new ServiceException("根据系统服务ID获取系统服务信息出现异常");
			}
			if(serviceInfo!=null){
				cacheService.putData("插入缓存业务系统服务对象，系统ID:" + sysId, KEY_VALUE_TYPE.SYS_ID__SYSSERVICE, sysId, serviceInfo);
			}
		}
		return serviceInfo;
	}*/

	/*@Override
	public SysServiceInfo getSysServiceInfoBySysCode(String sysCode) throws ServiceException {
		TurboCacheService cacheService = turboCacheService;
		SysServiceInfo serviceInfo = (SysServiceInfo) cacheService.getData("根据系统code获取系统服务",KEY_VALUE_TYPE.SYS_CODE__SYSSERVICE, sysCode);
		if (serviceInfo == null) {
			try {
				serviceInfo = sysServiceInfoService.getBySysCode(sysCode);
			} catch (ServiceException e) {
				throw e;
			}
			
			if(serviceInfo != null){
				cacheService.putData("插入缓存业务系统服务对象，系统code:" + sysCode, KEY_VALUE_TYPE.SYS_CODE__SYSSERVICE, sysCode,serviceInfo);
			}
		}
		return serviceInfo;
	}*/

	@Override
	public boolean hasExchRel(String fromNode, String toNode) throws ServiceException {
		try {
			NodeInfo from = getNodeInfoByCode(fromNode);
			NodeInfo to = getNodeInfoByCode(toNode);
			if (from != null && to != null) {
				return true;
			}
		} catch (ServiceException e) {
			throw e;
		}
		return false;
	}

	public AdapterBase getAdapterBase(String adaptorCode) throws ServiceException{
		return adapterBaseService.getByUniqueProperty("ADAPTER_CODE", adaptorCode);
	}
	
	
	//判断两个节点编码中间的网段类型，如果两个网段不一致则说明需要跨网交换
	/*@Override
	public boolean isCrossNet(String fromSys, String toSys) throws ServiceException {
		SysInfo fromSysInfo = this.getSysInfoByCode(fromSys);
		SysInfo toSysInfo = this.getSysInfoByCode(fromSys);
		NodeInfo fromNode = this.getNodeInfoById(fromSysInfo.getNodeId());
		NodeInfo toNode = this.getNodeInfoById(toSysInfo.getNodeId());
		if (fromNode != null && toNode != null) {
			String fromNetType = StrUtil.substring(fromNode.getNodeCode(), 6, 7);
			String toNetType = StrUtil.substring(fromNode.getNodeCode(), 6, 7);
			if (!fromNetType.equals(toNetType)) {
				return true;
			}
		}
		return false;
	}*/

	@Override
	public PipeLineFtp getFtpPipeLineByNodeCode(String nodeCode) throws ServiceException {
		TurboCacheService cacheService = turboCacheService;
		PipeLineFtp pipeLineFtp = (PipeLineFtp) cacheService.getData("根据系统节点code获取ftp配置信息",KEY_VALUE_TYPE.NODE_CODE__PIPELINE_FTP, nodeCode);
		if (pipeLineFtp == null) {
			try {
				NodeInfo nodeInfo = getNodeInfoByCode(nodeCode);
				pipeLineFtp = nodePipelineService.getFtpConfByNodeId(nodeInfo.getId());
			} catch (ServiceException e) {
				throw e;
			}
			
			if(pipeLineFtp != null){
				cacheService.putData("插入缓存节点ftp配置信息对象，节点code:" + nodeCode, KEY_VALUE_TYPE.NODE_CODE__PIPELINE_FTP,nodeCode, pipeLineFtp);
			}
		}
		return pipeLineFtp;
	}

	@Override
	public GapInfo getCurrentGapInfo() throws ServiceException {
		GapInfo gapInfo = null;
		try {
			List<GapInfo> gapInfoList = gapInfoService.list(null);
			if (CollectionUtil.hasElement(gapInfoList)) {
				gapInfo = gapInfoList.get(0);
			}
		} catch (Exception e) {
			logger.error("获取当前节点网闸信息出现异常!", e);
		}
		return gapInfo;
	}

	@Override
	public String getCurrentGapNode() throws ServiceException {
		String nodeCode = "";
		GapUserConf gapUserConf = null;
		try {
			List<GapUserConf> gapUserConfList = gapUserConfService.list(null);
			if (CollectionUtil.hasElement(gapUserConfList)) {
				gapUserConf = gapUserConfList.get(0);
				nodeCode = gapUserConf.getGapOwner();
			}
		} catch (Exception e) {
			logger.error("获取当前节点网闸配置信息出现异常!", e);
		}
		return nodeCode;
	}
	
	/**
	 * 根据节点编码，系统编码，适配器编码获取对应的适配器信息
	 * @param nodeCode
	 * @param sysCode
	 * @param adapterCode
	 * @return
	 */
	public AdapterInfo getAdapterInfo(String nodeCode,String adapterCode)  throws ServiceException{
		try {
			return adapterInfoService.getAdapterInfo(nodeCode, adapterCode);
		} catch (Exception e) {
			logger.error("获取适配器信息失败!", e);
		}
		return null;
		
	}
	
	
	/**
	 * 根据数据编码获取数据分类信息
	 * @param dataCode
	 * @return
	 * @throws ServiceException 
	 */
	public DataCategory getDataCategoryByCode(String dataCode) throws ServiceException{
		try {
			return dataCategoryService.getByUniqueProperty("data_code", dataCode);
		} catch (Exception e) {
			logger.error("根据数据编码获取数据分类信息失败!", e);
		}
		return null;
	}
	

	
	/**
	 * 对于跨网的，判断是走网闸还是刻盘
	 * 
	 * 如果返回null则刻盘
	 * 否则返回的节点就是网闸节点
	 * @return
	 */
	public String getGapNode(NodeInfo localNode){
		try {
			if (AppConst.YES.equals(localNode.getHasGap())) {//如果有网闸
				return localNode.getNodeCode();
			}
			//初始化网闸节点
			gapOwnerConfService.initGapConf();
			GapUserConf conf=gapUserConfService.getDefaultGap();
			if(conf!=null){//可用网闸
				return conf.getGapOwner();
			}else{//刻盘
				return null;
			}
		} catch (ServiceException e) {
			logger.error(e);
		}
		
		
		return null;
	}
}
